// zh_cjdianc/shopList/shopList.js
var a = getApp(),
  util = require("../utils/util.js");
var QQMapWX = require('../utils/qqmap-wx-jssdk.min.js');

// 实例化API核心类
var demo = new QQMapWX({
  key: 'EMBBZ-G57HV-LLYPP-U6UUP-VZNTK-KSF5N' // 必填
});

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 轮播图
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    bannerArrs: [{
      thumb: '../fq_img/shopList_banner.png'
    }, {
      thumb: '../fq_img/shopList_banner.png'
    }, {
      thumb: '../fq_img/shopList_banner.png'
    }],
    // 星星
    flag0: 3,
    storeData: [],
    xf: '',
    people: '',
    keyword: '',
    popErrorMsg: '',
    page: 1,
    type: 0,
    latitude: '',
    longitude: '',
    city: '',
    index_id: '0'
  },
  goDetail: function(e) {
    wx.showLoading({ //需要调用隐藏方法
      title: '加载中...',
      mask: true
    })
    wx.navigateTo({
      url: 'shopDetail?sortName=' + e.currentTarget.dataset.name + '&id=' + e.currentTarget.dataset.id,
      success: function(res) {
        wx.hideLoading() //隐藏加载框
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  tapxf: function(e) {
    this.setData({
      xf: e.detail.value
    })
  },
  tappeople: function(e) {
    this.setData({
      people: e.detail.value
    })
  },
  tapkey: function(e) {
    this.setData({
      keyword: e.detail.value
    })
  },
  tapName: function(event) {
    console.log(event)
    var id = event.currentTarget.id;
    var n = this;
    var xf = n.data.xf;
    var keyword = n.data.keyword;
    var people = n.data.people;
    var lat = n.data.latitude;
    var lng = n.data.longitude;
    a.util.request({
      url: "entry/wxapp/newStoreList",
      cachetime: "0",
      data: {
        keywords: keyword,
        xf_price: xf,
        xf_people: people,
        type: id,
        lat: lat,
        lng: lng
      },
      success: function(t) {
        console.log(t.data)
        n.setData({
          storeData: t.data.data.list,
          type: id,
          page: 1,
          index_id: id
        });
      }
    });
  },
  tapselect: function(event) {
    var n = this;
    var xf = n.data.xf;
    var keyword = n.data.keyword;
    var people = n.data.people;
    var type = n.data.type;
    var lat = n.data.latitude;
    var lng = n.data.longitude;
    if (xf && isNaN(xf)) {
      n.setData({
        popErrorMsg: "请输入正确的消费金额"
      })
      n.ohShitfadeOut();
      return;
    }
    if (people && isNaN(people)) {
      n.setData({
        popErrorMsg: "请输入正确的人数"
      })
      n.ohShitfadeOut();
      return;
    }
    if ((xf && !people) || (!xf && people)) {
      n.setData({
        popErrorMsg: "预计消费和到店人数必须同时输入"
      })
      n.ohShitfadeOut();
      return;
    }
    a.util.request({
      url: "entry/wxapp/newStoreList",
      cachetime: "0",
      data: {
        keywords: keyword,
        xf_price: xf,
        xf_people: people,
        type: type,
        lat: lat,
        lng: lng
      },
      success: function(t) {
        console.log(t.data)
        n.setData({
          storeData: t.data.data.list,
          page: 1
        });
      }
    });
  },
  ohShitfadeOut() {
    var fadeOutTimeout = setTimeout(() => {
      this.setData({
        popErrorMsg: ''
      });
      clearTimeout(fadeOutTimeout);
    }, 3000);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var n = this;
    a.setNavigationBarColor(this), a.pageOnLoad(this);
    // 底部菜单导航栏
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: function(res) {
        var latitude = res.latitude //维度
        var longitude = res.longitude //经度
        console.log(res);
        n.setData({
          latitude: latitude,
          longitude: longitude
        })
        a.util.request({
          url: "entry/wxapp/newStoreList",
          cachetime: "0",
          data: {
            lat: latitude,
            lng: longitude
          },
          success: function(t) {
            console.log(t.data)
            n.setData({
              storeData: t.data.data.list,
              page: 1
            });
          }
        });
        a.util.request({
          url: "entry/wxapp/newgetindeximg",
          cachetime: "0",
          data: {
            status: 2
          },
          success: function(t) {
            n.setData({
              bannerArrs: t.data.data.list,
            });
          }
        });
        demo.reverseGeocoder({    
          location: {
            latitude: latitude,
            longitude: longitude    
          },
              success: function(res) {        
            console.log(res);
            var city = res.result.address_component.city;
            n.setData({
              city: city
            })    
          },
              fail: function(res) {        
            console.log(res);
            var city = res.result.address_component.city;
            n.setData({
              city: city
            })    
          },
              complete: function(res) {        
            console.log(res);    
          }
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    var n = this;
    var xf = n.data.xf;
    var keyword = n.data.keyword;
    var people = n.data.people;
    var type = n.data.type;
    var lat = n.data.latitude;
    var lng = n.data.longitude;
    a.util.request({
      url: "entry/wxapp/newStoreList",
      cachetime: "0",
      data: {
        keywords: keyword,
        xf_price: xf,
        xf_people: people,
        type: type,
        lat: lat,
        lng: lng
      },
      success: function(t) {
        n.setData({
          storeData: t.data.data.list,
          page: 1
        });
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();
      }
    });
  },
  onReachBottom: function() {
    var n = this;
    var xf = n.data.xf;
    var keyword = n.data.keyword;
    var people = n.data.people;
    var page = n.data.page;
    var type = n.data.type;
    var lat = n.data.latitude;
    var lng = n.data.longitude;
    page = page + 1;

    // 显示加载图标
    wx.showLoading({
      title: '玩命加载中',
    })
    a.util.request({
      url: "entry/wxapp/newStoreList",
      cachetime: "0",
      data: {
        keywords: keyword,
        xf_price: xf,
        xf_people: people,
        type: type,
        page: page,
        lat: lat,
        lng: lng
      },
      success: function(t) {
        // 回调函数
        var storeData = n.data.storeData;

        for (var i = 0; i < t.data.data.list.length; i++) {
          storeData.push(t.data.data.list[i]);
        }
        // 设置数据
        n.setData({
          storeData: storeData,
          page: page
        });
        // 隐藏加载框
        wx.hideLoading();
      }
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})